<template>
  <div class="user_box">
    <div class="user_container">
      <h2>用户注册</h2>
      <!-- 注册对话框 -->
      <el-form
        :model="registerForm"
        :rules="registerFormRules"
        ref="registerFormRef"
        label-width="0px"
        class="user_form"
      >
        <el-form-item
          label=""
          prop="email"
        >
          <el-input
            v-model="registerForm.email"
            prefix-icon="el-icon-message"
            placeholder="请输入邮箱"
          ></el-input>
        </el-form-item>
        <el-form-item
          label=""
          prop="pass"
        >
          <el-input
            type="password"
            v-model="registerForm.pass"
            autocomplete="off"
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item
          label=""
          prop="checkPass"
        >
          <el-input
            type="password"
            v-model="registerForm.checkPass"
            autocomplete="off"
            prefix-icon="el-icon-lock"
            placeholder="确认密码"
          ></el-input>
        </el-form-item>
        <el-form-item class="user_item">
          <el-button type="primary" class="user_btn" @click="handleReg">注册</el-button>
        </el-form-item>
        <el-form-item class="go_user">
          已有账户,
          <a href="javascript:;" @click="goLogin">直接登录</a>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import '../assets/css/user.css'

export default {
  data () {
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.registerForm.checkPass !== '') {
          this.$refs.registerFormRef.validateField('checkPass')
        }
        callback()
      }
    }
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.registerForm.pass) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      // 注册表单对象
      registerForm: {
        email: '',
        pass: '',
        checkPass: ''
      },
      // 注册表单校验规则
      registerFormRules: {
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ],
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  created () {},
  methods: {
    // 点击按钮注册
    handleReg () {
      // 表单预校验
      this.$refs.registerFormRef.validate(async valid => {
        if (!valid) return
        // 通过校验发送注册请求
        const { data: res } = await this.$http.post('http://127.0.0.1:3000/register', {
          email: this.registerForm.email,
          psw: this.registerForm.pass
        })
        console.log(res)
        if (res.err_code !== 0) {
          return this.$message.error({
            duration: 800,
            message: '用户已存在，注册失败！'
          })
        }

        window.sessionStorage.setItem('token', JSON.stringify(res.token))
        // 注册成功提示
        this.$message.success({
          duration: 800,
          message: '注册成功'
        })
        // 成功返回首页
        this.$router.push('/')
      })
    },
    // 直接登录
    goLogin () {
      this.$router.push('/login')
    }
  }
}
</script>

<style lang="less" scoped>
</style>
